package com.person.base.action;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Preparable;
import com.person.base.model.BaseDept;
import com.person.base.model.BaseRole;
import com.person.base.model.BaseUnit;
import com.person.base.model.BaseUser;
import com.person.base.service.BaseDeptService;
import com.person.base.service.BaseRoleService;
import com.person.base.service.BaseUnitService;
import com.person.base.service.BaseUserService;
import com.person.commons.struts2.BaseAction;
import com.person.commons.utils.Constants;
import com.person.commons.utils.FlipPageInfo;
import com.person.sys.model.SysFace;
import com.person.sys.model.SysFunc;
import com.person.sys.service.SysFaceService;
import com.person.sys.service.SysLogService;
import com.person.title.model.TitleClass;
import com.person.title.model.TitleFunc;

public class BaseUserAction extends BaseAction implements Preparable{
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';
	private static final String USERLOGIN = "userLogin";
	private BaseUserService baseUserService;
	private SysLogService sysLogService;
	public BaseDeptService baseDeptService;
	public BaseRoleService baseRoleService;
	private BaseUnitService baseUnitService;
	private BaseUnit unit;
	public List<BaseDept> departments;
	public List<BaseRole> baseroles;
	public List<BaseUser> users;
	public List<BaseUser> users1;
	public BaseDept department;
	private FlipPageInfo pageData = new FlipPageInfo();
	private BaseUser user;
	private String code;//验证码
	private String userPwd;
	private String userPwd1;
	private String userPwd2;
	private BaseUser admin = new BaseUser();
	
	private SysFace face; 
	private SysFaceService sysFaceService;
	private String[] func ;
	
	private int flag;
	private String departId;
	private String roleId;
	private String spell;
	//add by wangzt
	private String mainRoleName;
	private String otherRoleName;
	private String funcstr;  //传入的页面地址
	private TitleClass cla;
	private List<TitleFunc> funcs;//总的Func列表
	private List<TitleClass> titles;//总的功能大类列表列表
	private List funcs0;//顶级的Func列表,加一个标志位，判断其是否为叶子节点
	private List funcs1;//第1级的Func列表,加一个标志位，判断其是否为叶子节点
	private List funcs2;//第2级的Func列表,加一个标志位，判断其是否为叶子节点
	private List funcs3;//第3级的Func列表,加一个标志位，判断其是否为叶子节点
	private List funcs4;//第4级的Func列表,加一个标志位，判断其是否为叶子节点
	public List getFuncs1() {
		return funcs1;
	}

	public void setFuncs1(List funcs1) {
		this.funcs1 = funcs1;
	}

	public List getFuncs2() {
		return funcs2;
	}

	public void setFuncs2(List funcs2) {
		this.funcs2 = funcs2;
	}

	public List getFuncs3() {
		return funcs3;
	}

	public void setFuncs3(List funcs3) {
		this.funcs3 = funcs3;
	}

	public List getFuncs4() {
		return funcs4;
	}

	public void setFuncs4(List funcs4) {
		this.funcs4 = funcs4;
	}

	
	
	//@JSON(name="cla")
	public TitleClass getCla() {
		return cla;
	}

	public void setCla(TitleClass cla) {
		this.cla = cla;
	}

	//@JSON(name="funcs")
	public List<TitleFunc> getFuncs() {
		return funcs;
	}

	public void setFuncs(List<TitleFunc> funcs) {
		this.funcs = funcs;
	}

	public String getFuncstr() {
		return funcstr;
	}

	public void setFuncstr(String funcstr) {
		this.funcstr = funcstr;
	}

	public String chooseUser(){
		
		return "chooseUser";
	}
	
	public String findDepartment(){
		if(flag == 1){
			if(!StringUtils.isNotBlank(departId)){
				users1 = baseUserService.searchAllEffectByDept();
			}else{
				users1 = baseUserService.searchEffectByDept(departId);
			}
		}else if(flag == 2){
			if(!StringUtils.isNotBlank(roleId)){
				users = baseUserService.searchAllEffectByDept();
			}else{
			users = baseUserService.searchEffectByRole(roleId);
			}
		}
		return "json";
	}
	
	
	public String inntDepartment(){
		departments = baseDeptService.searchAllDepart();
		return "json";
	}
	
	public String inntRole(){
		baseroles = baseRoleService.roleList();
		return "json";
	}
	
	/**
	 * 不包括离职的,但是显示所有的
	 */
	@SkipValidation
	public String allspellList() {
		
		users = baseUserService.getAllBySpelllist(spell);
		
		return "json";
	}
	
	/**
	 * 根据身份证来初始化
	 * @return
	 */
	
	public String innitByCode(){
		
		user = baseUserService.innitByCode(user.getUserCard());
		
		return "json";
	}
	
	
	
	/**
	 * 用户登录
	 * @author liuying
	 * 
	 */
	
	public String login(){
		String forward = "";
		
		String session_checkcode = (String) session.get(Constants.CHECK_CODE);
		if (session_checkcode!= null&&!session_checkcode.equalsIgnoreCase(code)) {
			info = getText("user.login.code");//验证码错误提示
			 forward = INPUT;
		}else{
			char state = baseUserService.login(user, request);
				if(state == '1'){
					//记录日志
					BaseUser user = (BaseUser)request.getSession().getAttribute(Constants.SESSION_USER);
					/**
					 * 设置系统IE标题栏
					 * add by wangzt
					 */
					//获得界面设置的参数
					face=sysFaceService.search();
                                       //end add by wangzt

                    forward = USERLOGIN;
				}else if(state == '0'){
					info = getText("user.login.userLogin");//登陆帐号不存在提示
					forward = INPUT;
				}else if(state == '2'){
					info = getText("user.login.userPwd");//密码错误提示
					forward = INPUT;
				}else if(state == '3'){
					info = getText("user.login.illUser");//无效用户提示
					forward = INPUT;
				}
		}
		return forward;
	}
	
	
	
	/**
	 * 在后台中英文转换时，整个框架刷新
	 * @author wangzt
	 * 
	 */
	
	public String changeFresh(){
		   String forward = "";
		   request.getSession().setAttribute(Constants.SESSION_USER,baseUserService.searchUserById(user.getUserId()));
		  //获得界面设置的参数
		  face=sysFaceService.search();
          forward = USERLOGIN;
		  return forward;
	}
	
	/**
	 * 用户管理的首页
	 * @return
	 */
	
	public String index(){
		
		return "index";
	}
	
	
	public String top(){
		
		return "top";
	} 
	
	/**
	 * 注册未审核用户的审核管理页面
	 * @return
	 */
	
	public String preSeachUser(){
		
		return "seachUser";
	}
	/**
	 * 注册未审核用户的审核管理页面
	 * @return
	 */
	
	public String seachUser(){
		
		pageData.setSize(20);
		
		pageData = baseUserService.searchEffectByUser(user, pageData);
		
		return "seachUserList";
	}
	
	
	public String auditIndex(){
		
		return "auditIndex";
	}
	
	
	public String auditLeft(){
		
		users = baseUserService.searchAllInAudit();
		users1 =  baseUserService.searchAllInEffectByDept();
		return "auditLeft";
	}
	
	
	public String auditRight(){
		
		users = baseUserService.searchAllInAudit();
		if(users.size() > 0){
			
			user = baseUserService.searchUserById(users.get(0).getUserId());
		}
		
		return "auditRight";
	}
	
	
	public String preAudit(){
		
			
		user = baseUserService.searchUserById(user.getUserId());
		
		
		return "auditRight";
	}
	
	
	public String audit(){
		
		if(StringUtils.isNotBlank(user.getUserId())){
			
			user = baseUserService.findUserById(user.getUserId());
			
			user.setUserFlag("0");
			
			baseUserService.createLog(admin, getLoginIp(request), "审核用户:"+user.getUserName()+"成功!", "14");
		}
		return auditRight();
	}
	
	/**
	 * 弹出选择更多的部门页面
	 * @return
	 */
	public String chooseDepts(){
		
		departments = baseDeptService.searchAllDepart();
		return "chooseDepts";
	}
	
	/**
	 * 弹出选择更多的角色页面
	 * @return
	 */
	public String chooseRoles(){
		
		baseroles = baseRoleService.roleList();
		return "chooseRoles";
	}
	
	
	/**
	 * 预添加用户
	 * @author liuying
	 * @return
	 */
	
	public String preCreateUser(){
		String forward= "";
		
		if(user != null){
			if(user.getUserFlag().equals("2")){//注册用户,状态为未审核
				
				forward = "registerUser";//转向到注册页面
				
			}else if(user.getUserFlag().equals("0")){//管理员在后台添加用户
				
				forward = "createUser";//转向到添加用户页面
			}
		}
		return forward;
	}
	
	
	/**
	 * 用户注册
	 * @author liuying
	 * @return
	 */
	public String register(){
		user.setUserFlag("2");
		user.setUserOrder(0);
		char state = baseUserService.create(user,request);
		if(state == SUCCESS){
			info = "注册成功!";
			
			baseUserService.createLog(user, getLoginIp(request), "注册用户:"+user.getUserName()+"成功!", "6");
			return "register_success";
		}else{	
			info = getText("user.register.account")+"\""+user.getUserLogin()+"\""+getText("user.register.has.account");
			user = new BaseUser();
			user.setUserFlag("2");
			return preCreateUser();
		}
		
		
	}
	
	
	/**
	 * 添加新用户
	 * @author liuying
	 * @return
	 */
	public String create(){
		user.setUserFlag("0");
		
		char state = baseUserService.create(user,request);
			
		if(state == SUCCESS){
			info = "添加成功!";
			user = new BaseUser();
			user.setUserFlag("0");
			flag = 1;
			
			baseUserService.createLog(admin, getLoginIp(request), "添加用户:"+user.getUserName()+"成功!", "6");
			return "create_success";
		}else{
			info = getText("user.register.account")+"\""+user.getUserLogin()+"\""+getText("user.register.has.account");
			user = new BaseUser();
			user.setUserFlag("0");
			return preCreateUser();
		}
		
		
	}
	
	
	public String userTree(){
		users = baseUserService.getAllNoDept();
		if(users.size() > 0){
			for (int i = 0; i < users.size(); i++){
				BaseUser user = users.get(i);
				if(user.getRoleId().equals("1")){
					users.remove(user);
					break;
				}
			}
		}
		return "userTree";
	}
	
	
	public String userInDepartTree(){
		
		info = baseUserService.userInDepartTree(department.getDeptParent(), request);
		return "tree";
	}
	
	
	public String userDepartTree(){
		
		info = baseUserService.userDepartTree(department.getDeptParent(), request);
		return "tree";
	}
	
	/**
	 * 某个部门下的所有用户
	 * @return
	 */
	
	public String managerUser(){
		pageData.setSize(20);
		
		pageData = baseUserService.searchEffectByDept(user.getDeptId(), pageData);
		
		department = baseDeptService.searchByDepartId(user.getDeptId());
		return "managerUser";
	}
	
	
	public String preModifyUser(){
		
		user = baseUserService.searchUserById(user.getUserId());
		return "modifyUser";
	}
	
	public String modify(){
		
		
		char status = baseUserService.modify(user,request);
		
		if(status == SUCCESS){
			info = "修改成功!";
			flag = 1;
			
			baseUserService.createLog(admin, getLoginIp(request), "修改用户:"+user.getUserName()+"成功!", "14");
			return "modify_success";
		}else{
			info = getText("user.register.account")+"\""+user.getUserLogin()+"\""+getText("user.register.has.account");
			user = baseUserService.findUserById(user.getUserId());
			return preCreateUser();
		}
	}
	
	public String clearPsw(){
		user = baseUserService.searchUserById(user.getUserId());
		user.setUserPwd("");
		baseUserService.modify(user,request);
		
		info = "密码已经清空!";
		
		baseUserService.createLog(admin, getLoginIp(request), "清空用户:"+user.getUserName()+"的密码!", "14");
		return managerUser();
	}
	
	
	public String searchUser(){
		
		return "userList";
	}
	
	/**
	 * 控制面板
	 * @return
	 */
	public String controlPanel(){
		
		return "controlPanel";
	}
	
	public String controlPanelLeft(){
		
		return "controlPanelLeft";
	}
	
	public String preModifyMyInfo(){
		
		user = (BaseUser)request.getSession().getAttribute(Constants.SESSION_USER);
		return "modifyMyInfo";
	}
	
	public String preModifyMyAccount(){
		user = (BaseUser)request.getSession().getAttribute(Constants.SESSION_USER);
		
	 //System.out.println(user.getUserId());
		user = baseUserService.searchUserById(user.getUserId());
		
		mainRoleName=baseRoleService.findByRoleId(user.getRoleId()).getRoleName();
		StringBuffer sb=new StringBuffer();
		if(null!=user.getRoleIdOther()&&!"".equals(user.getRoleIdOther())){
			  StringTokenizer commaToker = new StringTokenizer(user.getRoleIdOther(), ",");
			String[] str=new String[commaToker.countTokens()];
			  while (commaToker.hasMoreTokens()) {
				  String stemp=baseRoleService.findByRoleId(user.getRoleId()).getRoleName();
			   sb.append(commaToker.nextToken());
			   sb.append("/");
			  }
		}
		otherRoleName=sb.toString();
		
		return "modifyMyAccount";
	}
	
	public String preModifyPsw(){
	  
		user = (BaseUser)request.getSession().getAttribute(Constants.SESSION_USER);
		
		return "modifyPsw";
	}
	
	
	public String modifyPsw(){
		
		String forward = "";
		char state = baseUserService.updatePassWord(userPwd, userPwd1, userPwd2, user.getUserId(),request);
		
		if(state == '1'){
			info = getText("user.updatePsw.success");//修改密码成功
			forward = preModifyPsw();
			
			baseUserService.createLog(admin, getLoginIp(request), "修改密码成功!", "14");
		}else if(state == '2'){
			
			info = getText("user.updatePsw.notSame");//两次输入新密码不一致提示
			forward = INPUT;
		}else if(state == '3'){
			
			info = getText("user.updatePsw.wrongPsw");//原始密码输入错误提示
			forward = INPUT;
		}
		
		return forward;
	}
	
	
	public String deleteUser(){
		
		if(StringUtils.isNotBlank(user.getUserId())){
			
			
			user = baseUserService.findUserById(user.getUserId());
			
			char status = baseUserService.deleteUser(user,request);
			if(status == '1'){
				info = getText("user.delete.success");
				flag = 1;
				
				baseUserService.createLog(admin, getLoginIp(request), "删除用户:"+user.getUserName()+"成功!", "8");
			}else{
				info = getText("user.delete.falure");
				baseUserService.createLog(admin, getLoginIp(request), "删除用户:"+user.getUserName()+"失败!", "8");
			}
		}
		return "delete_succ";
	}
	
	
	 @JSON(name="baseUser")
	public BaseUser getUser() {
		return user;
	}

	public void setUser(BaseUser user) {
		this.user = user;
	}


	@JSON(name="departments")
	public List<BaseDept> getDepartments() {
		return departments;
	}

	public void setDepartments(List<BaseDept> departments) {
		this.departments = departments;
	}

	@JSON(name="baseroles")
	public List<BaseRole> getBaseroles() {
		return baseroles;
	}

	public void setBaseroles(List<BaseRole> baseroles) {
		this.baseroles = baseroles;
	}

	@JSON(name="users")
	public List<BaseUser> getUsers() {
		return users;
	}

	public void setUsers(List<BaseUser> users) {
		this.users = users;
	}
	
	@JSON(name="users1")
	public List<BaseUser> getUsers1() {
		return users1;
	}
	
	public void setUsers1(List<BaseUser> users1) {
		this.users1 = users1;
	}
	
	
	public BaseDept getDepartment() {
		return department;
	}

	public void setDepartment(BaseDept department) {
		this.department = department;
	}

	public FlipPageInfo getPageData() {
		return pageData;
	}

	public void setPageData(FlipPageInfo pageData) {
		this.pageData = pageData;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getUserPwd1() {
		return userPwd1;
	}

	public void setUserPwd1(String userPwd1) {
		this.userPwd1 = userPwd1;
	}

	public String getUserPwd2() {
		return userPwd2;
	}

	public void setUserPwd2(String userPwd2) {
		this.userPwd2 = userPwd2;
	}

	public SysFace getFace() {
		return face;
	}

	public void setFace(SysFace face) {
		this.face = face;
	}

	public void setBaseUserService(BaseUserService baseUserService) {
		this.baseUserService = baseUserService;
	}

	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}

	public void setBaseDeptService(BaseDeptService baseDeptService) {
		this.baseDeptService = baseDeptService;
	}

	public void setBaseRoleService(BaseRoleService baseRoleService) {
		this.baseRoleService = baseRoleService;
	}

	public void setSysFaceService(SysFaceService sysFaceService) {
		this.sysFaceService = sysFaceService;
	}

	public String[] getFunc() {
		return func;
	}

	public void setFunc(String[] func) {
		this.func = func;
	}
	
	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getDepartId() {
		return departId;
	}

	public void setDepartId(String departId) {
		this.departId = departId;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getSpell() {
		return spell;
	}

	public void setSpell(String spell) {
		this.spell = spell;
	}

	public BaseUser getAdmin() {
		return admin;
	}

	public void setAdmin(BaseUser admin) {
		this.admin = admin;
	}

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		admin = (BaseUser)request.getSession().getAttribute("user");
		departments = baseDeptService.departSelect();
		baseroles = baseRoleService.roleList();
		for (int i = 0; i < baseroles.size(); i++){
			BaseRole role = baseroles.get(i);
			if(role.getRoleId().equals("1")){
				baseroles.remove(role);
				break;
			}
		}
		unit = new BaseUnit();
		unit = baseUnitService.searchRecord();
	}
	
	public BaseUnit getUnit() {
		return unit;
	}

	public void setUnit(BaseUnit unit) {
		this.unit = unit;
	}

	public void setBaseUnitService(BaseUnitService baseUnitService) {
		this.baseUnitService = baseUnitService;
	}

	/**
	 * 获得IP地址
	 * 
	 * @return
	 */
	private String getLoginIp(HttpServletRequest request) {

		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}

		return ip;
	}
	/**
	 * 根据传入的URL，判断要添加的是属于那个功能的
	 */
	public String showFun(){
		List list=new ArrayList();
		String flag="0";//不是叶子节点
		List<TitleFunc> funcs=new ArrayList();
		List<TitleFunc> funccs1=new ArrayList();
		List<TitleFunc> funccs2=new ArrayList();
		List<TitleFunc> funccs3=new ArrayList();
		List<TitleFunc> funccs4=new ArrayList();
		
		if(null==funcstr||funcstr.length()==32){//在修改时（长度为32）,或在栏目管理（为空）时使用
			list=baseUserService.showFun(funcstr);
		}else{
			String array [] = funcstr.split("/");
			int length = array.length;
			String manustr = "/" + array[length-2] + "/" + array[length-1];
			String[] temps=manustr.split("\\.");//获得菜单地址
			if(null!=temps[0]&&temps[0].length()>0){
				list=baseUserService.showFun(temps[0]);
			}
		}
			if(null!=list&&list.size()>0){
				for(int i=0;i<list.size();i++){
					Object[] objs=(Object[])list.get(i);
					TitleFunc funcc=(TitleFunc) objs[0];
					
					//确定该栏目是属于哪一级的
					if(funcc.getClassCodeGroup().length()==4){
						funccs1.add(funcc);
					}else if(funcc.getClassCodeGroup().length()==6){
						funccs2.add(funcc);
					}else if(funcc.getClassCodeGroup().length()==8){
						funccs3.add(funcc);
					}else {
						funccs4.add(funcc);
					}
					funcs.add(funcc);
					if(i==0)
						cla=(TitleClass) objs[1];
				}
				//判断叶子节点
				funcs1=new ArrayList();
				for(int j=0;j<funccs1.size();j++){//判断一级节点是否是叶子节点（在数据库里应该添加关于叶子节点的字段，在保存栏目时，只需判断其父节点，将其父节点的叶子标志位改变即可）
					TitleFunc funcc1=funccs1.get(j);
					if(null==funccs2||funccs2.size()==0){//说明没有二级列表，一级均为叶子节点
						Object[] obj1=new Object[4];
						obj1[0]=funcc1.getFuncId();
						obj1[1]=funcc1.getClassCodeGroup();
						obj1[2]=funcc1.getFuncName();
						obj1[3]="1";
						funcs1.add(obj1);
					}
					else{
						flag="1";
						for(int k=0;k<funccs2.size();k++){//从二级列表中查找
							TitleFunc funcc2=funccs2.get(k);
							if(funcc2.getClassCodeGroup().indexOf(funcc1.getClassCodeGroup())!=-1){//从二级列表中找到其二级项目，说明一级不是叶子节点
								flag="0";
								break;
							}
						}
						if(flag=="1"){//一级是叶子节点
							
							Object[] obj1=new Object[4];
							obj1[0]=funcc1.getFuncId();
							obj1[1]=funcc1.getClassCodeGroup();
							obj1[2]=funcc1.getFuncName();
							obj1[3]="1";
							funcs1.add(obj1);
						}
						else{//一级不是叶子节点
							Object[] obj1=new Object[4];
							obj1[0]=funcc1.getFuncId();
							obj1[1]=funcc1.getClassCodeGroup();
							obj1[2]=funcc1.getFuncName();
							obj1[3]="0";
							funcs1.add(obj1);
						}
					}
				}
				
				funcs2=new ArrayList();
				for(int j=0;j<funccs2.size();j++){//判断二级节点是否是叶子节点
					TitleFunc funcc2=funccs2.get(j);					
					if(null==funccs3||funccs3.size()==0){//说明没有三级列表，二级均为叶子节点
						Object[] obj2=new Object[4];
						obj2[0]=funcc2.getFuncId();
						obj2[1]=funcc2.getClassCodeGroup();
						obj2[2]=funcc2.getFuncName();
						obj2[3]="1";
						funcs2.add(obj2);
					}else{
					flag="1";					
					for(int k=0;k<funccs3.size();k++){//从三级列表中查找
						TitleFunc funcc3=funccs3.get(k);
						if(funcc3.getClassCodeGroup().indexOf(funcc2.getClassCodeGroup())!=-1){//从三级列表中找到其二级项目，说明二级不是叶子节点
							flag="0";
							break;
						}
					}
					if(flag=="1"){//二级是叶子节点
						
						Object[] obj1=new Object[4];
						obj1[0]=funcc2.getFuncId();
						obj1[1]=funcc2.getClassCodeGroup();
						obj1[2]=funcc2.getFuncName();
						obj1[3]="1";
						funcs2.add(obj1);
					}
					else{//二级不是叶子节点
						Object[] obj2=new Object[4];
						obj2[0]=funcc2.getFuncId();
						obj2[1]=funcc2.getClassCodeGroup();
						obj2[2]=funcc2.getFuncName();
						obj2[3]="0";
						funcs2.add(obj2);
					}
					}
				}
				
				funcs3=new ArrayList();
				for(int j=0;j<funccs3.size();j++){//判断三级节点是否是叶子节点
					TitleFunc funcc3=funccs3.get(j);
					if(null==funccs4||funccs4.size()==0){//说明没有四级列表，三级均为叶子节点
						Object[] obj1=new Object[4];
						obj1[0]=funcc3.getFuncId();
						obj1[1]=funcc3.getClassCodeGroup();
						obj1[2]=funcc3.getFuncName();
						obj1[3]="1";
						funcs3.add(obj1);
					}else{
					flag="1";
					for(int k=0;k<funccs4.size();k++){//从四级列表中查找
						TitleFunc funcc4=funccs4.get(k);
						if(funcc4.getClassCodeGroup().indexOf(funcc3.getClassCodeGroup())!=-1){//从四级列表中找到其三级项目，说明三级不是叶子节点
							flag="0";
							break;
						}
					}
					if(flag=="1"){//三级是叶子节点
						
						Object[] obj1=new Object[4];
						obj1[0]=funcc3.getFuncId();
						obj1[1]=funcc3.getClassCodeGroup();
						obj1[2]=funcc3.getFuncName();
						obj1[3]="1";
						funcs3.add(obj1);
					}
					else{//说明三级不是叶子节点
						Object[] obj1=new Object[4];
						obj1[0]=funcc3.getFuncId();
						obj1[1]=funcc3.getClassCodeGroup();
						obj1[2]=funcc3.getFuncName();
						obj1[3]="0";
						funcs3.add(obj1);
					}
					}
				}
				//所有的四级节点都是叶子节点
				funcs4=new ArrayList();
				for(int j=0;j<funccs4.size();j++){//判断一级节点是否是叶子节点（在数据库里应该添加关于叶子节点的字段，在保存栏目时，只需判断其父节点，将其父节点的叶子标志位改变即可）
					TitleFunc funcc4=funccs4.get(j);
						Object[] obj1=new Object[4];
						obj1[0]=funcc4.getFuncId();
						obj1[1]=funcc4.getClassCodeGroup();
						obj1[2]=funcc4.getFuncName();
						obj1[3]="1";
						funcs4.add(obj1);
					}
		}
		return "resu";
	}
	
	
	
	
	/**
	 * 功能树
	 */
	public String manaFun(){
		List list=new ArrayList();
		String flag="0";//不是叶子节点
		List<TitleFunc> funcs=new ArrayList();
		List<TitleFunc> funccs0=new ArrayList();
		List<TitleFunc> funccs1=new ArrayList();
		List<TitleFunc> funccs2=new ArrayList();
		List<TitleFunc> funccs3=new ArrayList();
		List<TitleFunc> funccs4=new ArrayList();

		titles=baseUserService.searchTitle();
		list=baseUserService.showFun(funcstr);
			if(null!=list&&list.size()>0){
				for(int i=0;i<list.size();i++){
					Object[] objs=(Object[])list.get(i);
					TitleFunc funcc=(TitleFunc) objs[0];
					
					//确定该栏目是属于哪一级的
					if(funcc.getClassCodeGroup().length()==4){
						funccs1.add(funcc);
					}else if(funcc.getClassCodeGroup().length()==6){
						funccs2.add(funcc);
					}else if(funcc.getClassCodeGroup().length()==8){
						funccs3.add(funcc);
					}else {
						funccs4.add(funcc);
					}
					funcs.add(funcc);
					if(i==0)
						cla=(TitleClass) objs[1];
				}
				//判断叶子节点
				funcs1=new ArrayList();
				for(int j=0;j<funccs1.size();j++){//判断一级节点是否是叶子节点（在数据库里应该添加关于叶子节点的字段，在保存栏目时，只需判断其父节点，将其父节点的叶子标志位改变即可）
					TitleFunc funcc1=funccs1.get(j);
					if(null==funccs2||funccs2.size()==0){//说明没有二级列表，一级均为叶子节点
						Object[] obj1=new Object[4];
						obj1[0]=funcc1.getFuncId();
						obj1[1]=funcc1.getClassCodeGroup();
						obj1[2]=funcc1.getFuncName();
						obj1[3]="1";
						funcs1.add(obj1);
					}
					else{
						flag="1";
						for(int k=0;k<funccs2.size();k++){//从二级列表中查找
							TitleFunc funcc2=funccs2.get(k);
							if(funcc2.getClassCodeGroup().substring(0, 4).equals(funcc1.getClassCodeGroup())){//从二级列表中找到其二级项目，说明一级不是叶子节点
								flag="0";
								break;
							}
						}
						if(flag=="1"){//一级是叶子节点
							
							Object[] obj1=new Object[4];
							obj1[0]=funcc1.getFuncId();
							obj1[1]=funcc1.getClassCodeGroup();
							obj1[2]=funcc1.getFuncName();
							obj1[3]="1";
							funcs1.add(obj1);
						}
						else{//一级不是叶子节点
							Object[] obj1=new Object[4];
							obj1[0]=funcc1.getFuncId();
							obj1[1]=funcc1.getClassCodeGroup();
							obj1[2]=funcc1.getFuncName();
							obj1[3]="0";
							funcs1.add(obj1);
						}
					}
				}
				
				funcs2=new ArrayList();
				for(int j=0;j<funccs2.size();j++){//判断二级节点是否是叶子节点
					TitleFunc funcc2=funccs2.get(j);					
					if(null==funccs3||funccs3.size()==0){//说明没有三级列表，二级均为叶子节点
						Object[] obj2=new Object[4];
						obj2[0]=funcc2.getFuncId();
						obj2[1]=funcc2.getClassCodeGroup();
						obj2[2]=funcc2.getFuncName();
						obj2[3]="1";
						funcs2.add(obj2);
					}else{
					flag="1";					
					for(int k=0;k<funccs3.size();k++){//从三级列表中查找
						TitleFunc funcc3=funccs3.get(k);
						if(funcc3.getClassCodeGroup().substring(0, 6).equals(funcc2.getClassCodeGroup())){//从三级列表中找到其二级项目，说明二级不是叶子节点
							flag="0";
							break;
						}
					}
					if(flag=="1"){//二级是叶子节点
						
						Object[] obj1=new Object[4];
						obj1[0]=funcc2.getFuncId();
						obj1[1]=funcc2.getClassCodeGroup();
						obj1[2]=funcc2.getFuncName();
						obj1[3]="1";
						funcs2.add(obj1);
					}
					else{//二级不是叶子节点
						Object[] obj2=new Object[4];
						obj2[0]=funcc2.getFuncId();
						obj2[1]=funcc2.getClassCodeGroup();
						obj2[2]=funcc2.getFuncName();
						obj2[3]="0";
						funcs2.add(obj2);
					}
					}
				}
				
				funcs3=new ArrayList();
				for(int j=0;j<funccs3.size();j++){//判断三级节点是否是叶子节点
					TitleFunc funcc3=funccs3.get(j);
					if(null==funccs4||funccs4.size()==0){//说明没有四级列表，三级均为叶子节点
						Object[] obj1=new Object[4];
						obj1[0]=funcc3.getFuncId();
						obj1[1]=funcc3.getClassCodeGroup();
						obj1[2]=funcc3.getFuncName();
						obj1[3]="1";
						funcs3.add(obj1);
					}else{
					flag="1";
					for(int k=0;k<funccs4.size();k++){//从四级列表中查找
						TitleFunc funcc4=funccs4.get(k);
						if(funcc4.getClassCodeGroup().substring(0, 8).equals(funcc3.getClassCodeGroup())){//从四级列表中找到其三级项目，说明三级不是叶子节点
							flag="0";
							break;
						}
					}
					if(flag=="1"){//三级是叶子节点
						
						Object[] obj1=new Object[4];
						obj1[0]=funcc3.getFuncId();
						obj1[1]=funcc3.getClassCodeGroup();
						obj1[2]=funcc3.getFuncName();
						obj1[3]="1";
						funcs3.add(obj1);
					}
					else{//说明三级不是叶子节点
						Object[] obj1=new Object[4];
						obj1[0]=funcc3.getFuncId();
						obj1[1]=funcc3.getClassCodeGroup();
						obj1[2]=funcc3.getFuncName();
						obj1[3]="0";
						funcs3.add(obj1);
					}
					}
				}
				//所有的四级节点都是叶子节点
				funcs4=new ArrayList();
				for(int j=0;j<funccs4.size();j++){//判断一级节点是否是叶子节点（在数据库里应该添加关于叶子节点的字段，在保存栏目时，只需判断其父节点，将其父节点的叶子标志位改变即可）
					TitleFunc funcc4=funccs4.get(j);
						Object[] obj1=new Object[4];
						obj1[0]=funcc4.getFuncId();
						obj1[1]=funcc4.getClassCodeGroup();
						obj1[2]=funcc4.getFuncName();
						obj1[3]="1";
						funcs4.add(obj1);
					}
		}
		return "manaFun";
	}

	public List getFuncs0() {
		return funcs0;
	}

	public void setFuncs0(List funcs0) {
		this.funcs0 = funcs0;
	}

	public List<TitleClass> getTitles() {
		return titles;
	}

	public void setTitles(List<TitleClass> titles) {
		this.titles = titles;
	}
}

